/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.continew.admin.hanfu.core.oss.OSS;
import top.continew.admin.hanfu.domain.Carousel;
import top.continew.admin.hanfu.domain.OssFile;
import top.continew.admin.hanfu.domain.bo.CarouselBo;
import top.continew.admin.hanfu.domain.query.CarouselQuery;
import top.continew.admin.hanfu.domain.vo.CarouselVo;
import top.continew.admin.hanfu.mapper.CarouselMapper;
import top.continew.admin.hanfu.service.ICarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bbm
 * @since 2025-03-27
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements ICarouselService {
    private final OSS oss;

    private final CarouselMapper carouselMapper;

    /**
     * 查询
     */
    @Override
    public Page<CarouselVo> getCarouselListByCondition(CarouselQuery carouselQuery) {

        CarouselBo carouselBo = new CarouselBo();

        carouselBo.setSubject(carouselQuery.getSubject());
        carouselBo.setPageName(carouselQuery.getPageName());
        carouselBo.setUpdateBy(carouselQuery.getUpdateBy());

        return carouselMapper.getCarouselListByCondition(new Page<>(carouselQuery.getPageNum(), carouselQuery
            .getPageSize()), carouselBo);
    }

    @Override
    public int addCarousel(CarouselBo carousel) {

        Carousel c = new Carousel();

        List<OssFile> carouselImg = oss.saveFile("carouselImg", carousel.getUpdateBy() + "", carousel.getFile());

        c.setSubject(carousel.getSubject());
        c.setPageName(carousel.getPageName());
        //仅有一张图片
        c.setOssId(carouselImg.get(0).getOssId().toString());
        c.setUpdateBy(carousel.getUpdateBy());
        c.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = save(c);

        return save ? 1 : 0;
    }

    @Override
    public int updateCarousel(CarouselBo carouselBo) {

        log.info(String.valueOf(carouselBo));

        List<OssFile> carouselImg = null;
        if (carouselBo.getFile() != null) {
            carouselImg = oss.saveFile("carouselImg", carouselBo.getUpdateBy() + "", carouselBo.getFile());
        }

        boolean update = lambdaUpdate().eq(Carousel::getId, carouselBo.getId())
            .set(carouselBo.getSubject() != null, Carousel::getSubject, carouselBo.getSubject())
            .set(carouselBo.getUpdateBy() != null, Carousel::getUpdateBy, carouselBo.getUpdateBy())
            .set(carouselBo.getPageName() != null, Carousel::getPageName, carouselBo.getPageName())
            .set(!carouselBo.getFile().isEmpty(), Carousel::getOssId, carouselImg.get(0).getOssId())
            .set(Carousel::getUpdateTime, System.currentTimeMillis())
            .update();

        return update ? 1 : 0;
    }
}
